package com.shujia.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

public class Code06GetData {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {

        ZooKeeper zooKeeper = new ZooKeeper("master:2181,node1:2181,node2:2181"
                , 10 * 1000
                , new Watcher() {
            @Override
            // 该方法在Zookeeper连接中会被触发两次，一次在建立连接时，另外一次在关闭连接时
            // 该方法执行时，是按照异步执行
            public void process(WatchedEvent event) {
                System.out.println("当前Zookeeper连接被触发...");
            }
        }
        );

        String path = "/api";

        // 获取指定节点是否存在
        Stat stat = zooKeeper.exists(path, false);

        // public byte[] getData(String path, boolean watch, Stat stat)
        byte[] data = zooKeeper.getData(path, false, stat);

        String dataStr = new String(data);
        System.out.println(dataStr);

        zooKeeper.close();

    }
}
